package com.example.shop_2207.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.example.shop_2207.R
import com.example.shop_2207.adapter.GoodsAdapter
import com.example.shop_2207.adapter.OneAdapter
import com.example.shop_2207.adapter.TwoAdapter
import com.example.shop_2207.databinding.FragmentCateBinding
import com.example.shop_2207.entity.CategoryEntity
import com.example.shop_2207.entity.GoodsEntity
import com.example.shop_2207.mvvm.BaseFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CateFragment : BaseFragment<FragmentCateBinding,CateViewModel>() {
    val onelist = mutableListOf<CategoryEntity>()
    lateinit var oneAdapter: OneAdapter

    val twolist = mutableListOf<CategoryEntity>()
    lateinit var twoAdapter: TwoAdapter

    val threelist = mutableListOf<GoodsEntity>()
    lateinit var threeAdapter:GoodsAdapter

    var page = 1
    val size =10
    var cateId = 0

    override fun getLayout(): Int  = R.layout.fragment_cate

    override fun getViewModel(): Class<CateViewModel>  = CateViewModel::class.java

    override fun initView() {
        //初始化
        oneAdapter = OneAdapter(onelist)
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)

        twoAdapter = TwoAdapter(twolist)
        binding.rvTwo.adapter = twoAdapter
        binding.rvTwo.layoutManager = LinearLayoutManager(context)

        threeAdapter = GoodsAdapter(threelist)
        binding.rvThree.adapter = threeAdapter
        binding.rvThree.layoutManager = LinearLayoutManager(context)

        viewModel.cate()
        //一级分类点击事件
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            oneAdapter.setSelectedId(onelist[position].id)
            oneAdapter.notifyDataSetChanged()
            //加载二级分类
            //先清空二级和三级列表的数据
            twolist.clear()
            twoAdapter.notifyDataSetChanged()
            threelist.clear()
            threeAdapter.notifyDataSetChanged()
            //加载二列表数据
            val twoData = onelist[position].data
            if (twoData != null) {
                if(twoData.isNotEmpty()){
                    twolist.addAll(twoData)
                    twoAdapter.notifyDataSetChanged()
                    //加载三级列表
                    cateId = twolist[0].id
                    page=1
                    viewModel.goods(cateId,page,size)
                }
            }
        }
        //二级分类点击事件
        twoAdapter.setOnItemClickListener { adapter, view, position ->
            cateId = twolist[position].id
            //建议，只要点击了一级二级中的分类，page = 1
            page = 1
            viewModel.goods(cateId,page,size)
        }
        binding.smart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.goods(cateId,page,size)
                binding.smart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.goods(cateId,page,size)
                binding.smart.finishLoadMore()
            }
        })
        //三级列表点击事件
        threeAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity").withInt("goodsId",threelist[position].id).navigation()
        }
    }

    override fun initData() {
        viewModel.cateSuccess.observe(this){
            if(it.code==200){
                //填充一级分类数据
                onelist.clear()
                onelist.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //初始化二级分类数据
                val twoData = it.data[0].data
                if(twoData!=null){
                    twolist.clear()
                    twolist.addAll(twoData)
                    twoAdapter.notifyDataSetChanged()
                    //初始化三级列表数据
                    cateId = twolist[0].id
                    viewModel.goods(cateId,page,size)
                }
            }
        }

        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    threelist.clear()
                }
                threelist.addAll(it.data)
                threeAdapter.notifyDataSetChanged()
            }
        }

    }

}